<template>
	<view class="page-wraper" :style="'background-color:' + pageBac + ';'">
		<customNav title1="二手车" title2="寻车" bgColor="#4E6ECC" backIconColor="white" @clickTitle='clickTitle'>
		</customNav>
		<addressSearch ref="setAddres" @searchBtn='searchBtn' @inputPhone='inputPhone' @addressBtn='addressBtn' :addALLAddress="true">
		</addressSearch>
		<view class="" id="id_dingwei"></view>
		<view v-if="isShowList">
			<secondhandCar ref="secondhandCarRef" v-if="temp==1?true:false" :scroll_height="scroll_height"
				:isLogin="isLogin" />
			<searchCar ref="searchCarRef" :series_name="series_name" v-if="temp==2?true:false" :brandName="brandName"
				:series_id='series_id' :scroll_height="scroll_height" :isLogin="isLogin" @changeSeries='changeSeries' />
		</view>
	</view>
</template>
<script>
	import customNav from "@/components/custom-nav/custom-nav.vue";
	import secondhandCar from "@/secondhandCar/secondhandCar1.vue";
	import searchCar from "@/secondhandCar/searchCar.vue";
	export default {
		components: {
			customNav,
			secondhandCar,
			searchCar
		},
		data() {
			return {
				temp: 1,
				brandName: '',
				series_id: '',
				sreach: '', //寻车列表中搜索框的功能
				inputValue:'',
				scroll_height: '',
				isShowList: true,
				users_id: '',
				isLogin: true,
				pageBac: '#FFFFFF',
				series_name: '全部车型'
			}
		},
		onShow() {
			try{
				this.$utils.stopPage() //禁止页面本身的滑动
				this.$refs.setAddres.setAddres()
			}catch(e){
				//TODO handle the exception
			}
			
		},
		/**
		 * onHide()触发的场景：
		 *导航页1>>导航页2，会触发导航页1  onHide（）
		 *
		 *导航页>>子页面，会触发导航页  onHide（）
		 *
		 *子页面1>>子页面2，会触发子页面1  onHide（）
		 **/
		onHide() {
			this.$utils.movePage()
		},
		/**
		 *onUnload()触发的场景：
		 *从子页面2返回到子页面1，会触发子页面2的onUnload()
		 *
		 *从子页面返回到导航页，会触发子页面的onUnload()
		 */
		onUnload() {
			this.$utils.movePage()
		},
		onLoad() {
			const value = uni.getStorageSync('login')
			if (value) {
				this.users_id = value.id
				this.isLogin = true
			} else {
				this.isLogin = false
			}
		},
		onReady() {
			const query = uni.createSelectorQuery().in(this);
			query.select('#id_dingwei').boundingClientRect(data => {
				this.$utils.log("得到布局位置信息" + JSON.stringify(data));
				var that = this
				that.isShowList = false
				that.$nextTick(() => {
					that.scroll_height = uni.getSystemInfoSync().windowHeight - data.top + 'px';
					that.top_height = data.top + 20 + 'px'
					that.isShowList = true
					this.$utils.log("topgaodu   " + that.top_height);
					this.$utils.log("that.scroll_height  " + that.scroll_height);
				})

			}).exec();
		},
		methods: {
			changeSeries(id, name) {
				this.series_id = id
				this.series_name = name
			},
			//选择车型返回数据的方法
			backData(obj) {
				var that = this
				this.$utils.log("选择车型返回数据的方法" + JSON.stringify(obj))
				that.temp = 0
				this.$nextTick(() => {
					that.series_id = obj.id
					if (obj.name && obj.name != '不限') {
						that.series_name = obj.name
					} else {
						that.series_name = '全部车型'
					}
					that.$utils.log("选择车型返回数据的方法   " + that.series_name)
					that.temp = 2
				})
			},
			clickTitle(inta) {
				this.$utils.log("在index页面点了了title" + inta)
				if(this.temp != inta){
					this.$utils.log("不等   " + inta)
					this.sreach = ''
					this.$refs.setAddres.inputValueUpdata()
					this.inputPhone()
				}
				this.temp = inta
				
				if (this.temp == 1) {
					
					this.pageBac = "#FFFFFF"
				} else {
					this.$utils.log("在index页面点了了title" + inta)
					this.pageBac = "#F5F5F5"
				}
			},
			//搜索框搜索
			searchBtn(content) {
				this.sreach = content
				this.$utils.log("点击搜索" + content)
				var that = this
				if (this.temp == 2) {
					const loaction = uni.getStorageSync('address')
					if (loaction.id) {
						this.$refs.searchCarRef.searchCarCS(loaction.id, content)
					} else {
						this.$refs.searchCarRef.searchCarCS('', content)
					}

				} else {
					const loaction = uni.getStorageSync('address')
					if (loaction.id) {
						this.$refs.secondhandCarRef.secondhandCarCS(loaction.id, content)
					} else {
						this.$refs.secondhandCarRef.secondhandCarCS('', content)
					}
				}
			},
			//地址发生变化触发
			addressBtn(loaction) {
				this.$utils.log("地址发生变化触发")
				var that = this
				if (this.temp == 2) {
					if (loaction.id) {
						this.$refs.searchCarRef.searchCarCS(loaction.id, this.sreach)
					} else {
						this.$refs.searchCarRef.searchCarCS('', this.sreach)
					}

				} else {
					if (loaction.id) {
						this.$refs.secondhandCarRef.secondhandCarCS(loaction.id, this.sreach)
					} else {
						this.$refs.secondhandCarRef.secondhandCarCS('', this.sreach)
					}
				}
			},
			inputPhone() {
				this.sreach = ''
				var that = this
				if (this.temp == 2) {
					const loaction = uni.getStorageSync('address')
					if (loaction.id) {
						this.$refs.searchCarRef.searchCarCS(loaction.id, '')
					} else {
						this.$refs.searchCarRef.searchCarCS('', '')
					}
				} else {
					const loaction = uni.getStorageSync('address')
					if (loaction.id) {
						this.$refs.secondhandCarRef.secondhandCarCS(loaction.id, '')
					} else {
						this.$refs.secondhandCarRef.secondhandCarCS('', '')
					}
				}
			}
		}
	}
</script>

<style>
	.page-wraper {
		display: flex;
		flex-direction: column;
		width: 100%;
		height: auto;
	}
</style>